import { message as Message } from "ant-design-vue";
import { computed, Ref } from "vue";
import { pushOrdersTttxApi, pushCustomerDataApi } from "@/api/waybillManagement/customerServiceWaybill";
import { cancelBatchOrderShipmentApi } from "@/api/waybillManagement/businessWaybill";

export default function PushOrderHook(state: { tableRef: Ref<any>; activeKey: Ref<string>; searchHandle: () => void }) {
  const { tableRef, activeKey, searchHandle } = state;

  const selectRows = computed<Array<ClientWaybillManagement.ListRows>>(() => {
    return tableRef.value.xTable.getCheckboxRecords() || [];
  });

  // 创建运单
  const createShipmentHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    pushOrdersTttxApi({ idList: selectRows.value.map((t: any) => t.id), status: activeKey.value }).then((res: any) => {
      const { code } = res;
      if (code === 200) {
        Message.success("操作成功!");
        searchHandle();
      }
    });
  };

  //推送拣货数据
  const pushCustomerDataHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    pushCustomerDataApi({ idList: selectRows.value.map((t: any) => t.id) }).then((res: any) => {
      const { code } = res;
      if (code === 200) {
        Message.success("操作成功!");
        searchHandle();
      }
    });
  };

  // 取消运单
  const cancelShipmentHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    const ids = selectRows.value.map((t: any) => t.id);

    cancelBatchOrderShipmentApi(ids).then((res: any) => {
      const { code, data } = res;
      if (code === 200 && data === "取消成功") {
        Message.success("操作成功!");
        searchHandle();
      }
    });
  };

  return {
    cancelShipmentHandle,
    createShipmentHandle,
    pushCustomerDataHandle
  };
}
